package com.stars.home.utils

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.home.R
import com.stars.home.adapter.CarTypeAdapter
import com.stars.home.adapter.CarTypeToAdapter
import com.stars.home.inte.ChoiceListData
import com.stars.home.model.CarCategory
import com.trailer.base.utils.Toasts
import com.trailer.base.utils.UiUtils

/**
 * 选择板车类型
 *
 *on 2020/11/21
 */
object PopuCarTypeUtils {


    fun show(context: Context, list:List<CarCategory>,view: View, choicedata: ChoiceListData) {

        var type_id=""
        var typeName=""
        var fNmae=""
        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_car_type, null)
        val  ll:LinearLayout = layout.findViewById(R.id.ll)

        val recyclerView: RecyclerView = layout.findViewById(R.id.recycler_view)//父类

        val recyclerTo: RecyclerView = layout.findViewById(R.id.recycler_to)//子类


        val tvCancel: TextView = layout.findViewById(R.id.tv_cancel)
        val tvSure: TextView = layout.findViewById(R.id.tv_sure)

        recyclerView.addItemDecoration(GridSpaceItemDecoration(6,
                UiUtils.dp2Px(context, 10f),
                UiUtils.dp2Px(context, 10f)))

        recyclerTo.addItemDecoration(GridSpaceItemDecoration(6,
                UiUtils.dp2Px(context, 10f),
                UiUtils.dp2Px(context, 10f)))

        //todo:


        list[0].isChose=true
        fNmae=list[0].name

        var adapter: CarTypeAdapter = CarTypeAdapter()
        recyclerView.adapter = adapter
        adapter.addData(list)

        //子类第一个
        var adapterto: CarTypeToAdapter = CarTypeToAdapter()
        recyclerTo.adapter = adapterto
        adapterto.addData(list[0].datas)

        //初始值
        for(item in list ){
            if(item.isChose){
                fNmae=item.name
                for(to in item.datas){
                    if(to.isChose){
                        type_id=item.id
                        typeName=item.name
                    }
                }

            }
        }

        adapter.setOnItemClickListener { adapter, view, position ->

           var item= adapter.getItem(position) as CarCategory
            var list= adapter.data as List<CarCategory>
            for ((index, bean) in list.withIndex()) {

                if(index!=position){
                    if(bean.isChose){

                        //之前的子类选择的数据也置空
                        for(item in list[position].datas){
                            item.isChose=false
                        }
                        bean.isChose=false
                        adapter.notifyItemChanged(index)
                    }
                }
            }
            item.isChose=true
            fNmae=item.name
            type_id=""
            adapter.notifyItemChanged(position)
            //切换子类
            for(item in list[position].datas){
                item.isChose=false
            }
            adapterto.setList(list[position].datas)
        }


        adapterto.setOnItemClickListener { adapter, view, position ->

            var item= adapterto.getItem(position) as CarCategory.CarDatas
            var list= adapterto.data as List<CarCategory.CarDatas>
            for ((index, bean) in list.withIndex()) {

                if(index!=position){
                    if(bean.isChose){
                        bean.isChose=false
                        adapterto.notifyItemChanged(index)
                    }
                }
            }

            item.isChose=true
            type_id=item.id
            typeName=item.name
            adapterto.notifyItemChanged(position)


        }




        tvSure.onClick {

            if(TextUtils.isEmpty(type_id)){

               Toasts.toast(context,"请选择二级分类")
                return@onClick
            }
            if(null!=choicedata){
                choicedata.onRestuse(type_id, "$fNmae-$typeName")
            }
            mNormalPopup?.dismiss()
        }
        tvCancel.onClick {
            mNormalPopup?.dismiss()
        }

        ll.onClick {

            mNormalPopup?.dismiss()
        }

        builder.release()

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }


}
